﻿layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    //登录按钮
    form.on("submit(login)", function (data) {
        if ($("#userName").val() == "") {
            layer.msg("账号不能为空！");
            return false;
        }
        if ($("#password").val() == "") {
            layer.msg("密码不能为空！");
            return false;
        } else if ($("#password").val().length < 6 || $("#password").val().length > 20) {
            layer.msg("密码长度在6-20位之间");
            return false;
        }
        if ($("#code").val() == "") {
            layer.msg("请输入验证码！");
            return false;
        } else if ($("#code").val().length != 4) {
            layer.msg("验证码不规范！");
            return false;
        }
        $(this).text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");
        $.post("/Login/UserLogin/", {
            username: $("#userName").val(),
            password: $("#password").val(),
            captcha: $("#code").val()
        }, function (res) {
            if (res.isSuccess) {
                window.location.href = "/";
            } else {
                top.layer.msg(res.msg);
                $("#imgCheckCode").click();
                $("#btnLogin").text("登录").removeAttr("disabled").removeClass("layui-disabled");
                return false;
            }
        });
        return false;
    });
});
